package com.java.gmall.config;

import com.alibaba.dubbo.config.*;
import com.atguigu.gmall.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author jingjintao
 * @date 2021/8/22 - 18:27
 **/
@Configuration
public class MyDubboConfig {

/*<dubbo:application name="boot-user-service" ></dubbo:application>*/
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("boot-user-service");

        return applicationConfig;
    }

    /*<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />*/
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("127.0.0.1:2181");
        return registryConfig;
    }

//    <dubbo:protocol name="dubbo" port="20882" ></dubbo:protocol>
    @Bean
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        return protocolConfig;
    }

    /*<dubbo:service interface="com.atguigu.gmall.service.UserService"
    ref="userServiceImpl01" version="1.0.0" stub="com.atguigu.gmall.service.impl.UserServiceStub">
        <dubbo:method name="getUserAddressList" timeout="2000"></dubbo:method>
    </dubbo:service>*/
    @Bean
    public ServiceConfig<UserService> userServiceServiceConfig(UserService userService){
        ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<>();
        userServiceServiceConfig.setInterface(UserService.class);
        userServiceServiceConfig.setRef(userService);
        userServiceServiceConfig.setVersion("1.0.0");
        userServiceServiceConfig.setStub("com.atguigu.gmall.service.impl.UserServiceStub");

        //配置每一个method的信息
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("getUserAddressList");
        methodConfig.setTimeout(2000);

        //将method的设置关联到service配置中
        userServiceServiceConfig.setMethods(Arrays.asList(methodConfig));
        return userServiceServiceConfig;
    }

//    <dubbo:provider timeout="1000"></dubbo:provider>
    @Bean
    public ProviderConfig providerConfig(){
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(1000);
        return providerConfig;
    }

//    <dubbo:monitor protocol="registry"></dubbo:monitor>
    @Bean
    public MonitorConfig monitorConfig(){
        MonitorConfig monitorConfig = new MonitorConfig();
        monitorConfig.setProtocol("registry");
        return monitorConfig;
    }
}
